<?php
/**
 * ChmFile class file.
 *
 * @author Veaceslav Medvedev <slavcopost@gmail.com>
 * @link http://code.google.com/p/yiiext/
 * @license http://www.opensource.org/licenses/mit-license.php
 */

/**
 * ChmFile.
 *
 * @author Veaceslav Medvedev <slavcopost@gmail.com>
 * @version 0.1
 * @package yiiext.components.chm
 */
class EChmFile extends CComponent
{
	public function __construct($config=NULL)
	{
		$config=new CConfiguration($config);
		$config->applyTo($this);
	}
	public function getParams()
	{
		$compNames=array(
			// EChmHhpFile
			'autoIndex'=>'Auto Index',
			'autoToc'=>'Auto TOC',
			'binaryIndex'=>'Binary Index',
			'binaryToc'=>'Binary TOC',
			'citation'=>'CITATION',
			'compress'=>'COMPRESS',
			'copyright'=>'COPYRIGHT',
			'compiledFile'=>'Compiled file',
			'contentsFile'=>'Contents file',
			'createChiFile'=>'Create CHI file',
			'defaultFont'=>'Default Font',
			'defaultWindow'=>'Default Window',
			'defaultTopic'=>'Default topic',
			'displayCompileNotes'=>'Display compile notes',
			'displayCompileProgress'=>'Display compile progress',
			'enhancedDecompilation'=>'Enhanced decompilation',
			'errorLogFile'=>'Error log file',
			'fullTextSearchStopListFile'=>'Full text search stop list file',
			'fullTextSearch'=>'Full-text search',
			'ignore'=>'IGNORE',
			'indexFile'=>'Index file',
			'prefix'=>'PREFIX',
			'sampleStagingPath'=>'Sample Staging Path',
			'sampleListFile'=>'Sample list file',
			'tmpDir'=>'TMPDIR',
			'customTab'=>'Custom tab',
			// EChmHhcFile
			'Image Width'=>'imageWidth',
			'styles'=>'Window Styles',
			'extStyles'=>'ExWindow Styles',
			'autoGenerated'=>'Auto Generated',
			// EChmHhkItem
			'url'=>'URL',
			'seeAlso'=>'See Also',
		);
		$vars=array();
		foreach(get_object_vars($this) as $name=>$value)
		{
			if(substr($name,0,1)=='_' || !$value)
				continue;
			if(key_exists($name,$compNames))
				$name=$compNames[$name];

			$vars[ucfirst($name)]=$value;
		}
		return $vars;
	}
	public static function renderHtmlList($items)
	{
		$html='';
		if(count($items)>0)
		{
			$html.="<UL>\n";
			foreach($items as $item)
			{
				$html.="	<LI>".self::renderHtmlObject($item->getParams());
				if(count($item->children)>0)
					$html.=self::renderHtmlList($item->children);
			}
			$html.="</UL>\n";
		}

		return $html;
	}
	public static function renderHtmlObject($params,$type='text/sitemap')
	{
		$html='';
		if(count($params)>0)
		{
			$html.='<OBJECT type="'.$type.'">';
			foreach($params as $param=>$value)
				$html.='<param name="'.$param.'" value="'.$value.'">';
			$html.="</OBJECT>\n";
		}

		return $html;
	}
	public function render()
	{
		return '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">'."\n".
				'<HTML><HEAD>'."\n".
				'<META name="GENERATOR" content="yiiext.components.chm">'."\n".
				'</HEAD><BODY>'."\n".
				self::renderHtmlObject($this->getParams(),'text/site properties').
				self::renderHtmlList($this->getItems()).
				'</BODY></HTML>'."\n";
	}
	public function save($path,$sourceCharset='UTF-8')
	{
		if($sourceCharset!==FALSE)
			$contents=iconv($sourceCharset,"CP1251",$this->render());

		!file_exists($path) OR unlink($path);
		return file_put_contents($path,$contents,LOCK_EX);
	}
}
